class Tea {
    constructor(id, teaname, teaimg, teatype, teaprice) {
        this.id = id,
            this.teaname = teaname,
            this.teaimg = teaimg,
            this.teatype = teatype,
            this.teaprice = teaprice
    }
}
// 茶叶添加
Mock.mock("/js/teaadd", "post", function (obj) {
    // obj 为对象类型
    //获取本地存储
    let teastr = localStorage.Tea;
    // 此类型为字符串类型
    let teabodystr = obj.body;
    let teabody = JSON.parse(teabodystr);
    //判断本地存储是否为空
    if (teastr == "" || teastr == null || teastr == undefined || teastr == []) {
        let arr = [teabody];
        localStorage.Tea = JSON.stringify(arr);
        return true;
    } else {
        let bool = false;
        let taeObj = JSON.parse(teastr);
        for (let i = 0; i < taeObj.length; i++) {
            if (taeObj[i].teaname == teabody.teaname) {
                bool = true;
                break;
            }
        }
        if (bool) {
            return false;
        } else {
            teabody.id=1;
            let tea = new Tea(teabody.id, teabody.teaname, teabody.teaimg, teabody.teatype, teabody.teaprice);
            taeObj.push(tea);
            localStorage.Tea = JSON.stringify(taeObj);
            return true;
        }
    }

})



//茶叶回显
Mock.mock("/js/teashow", "post", function () {
    let bdcu = localStorage.Tea;
    let bodyobj = document.getElementsByClassName("teashow")[0];
    if (bdcu == "" || bdcu == null || bdcu == undefined || bdcu == []) {
        bodyobj.innerHTML = "<div><div style='color:bule'>暂无数据</div></div>";
        return false;
    } else {
        let bdcuobj = JSON.parse(bdcu)
        let a = "";
        for (let i = 0; i < bdcuobj.length; i++) {

// 给每一个茶赋予一个点击事件 i表示本地存储的下表
            a += `<div onclick='teaclick(\"${i}\")'>`
            a += ` <div class='teaimg'><img src='./img/${bdcuobj[i].teaimg}'></div>`
            a += `<li><a href="">${bdcuobj[i].teaname}</a><div class='teaunderline'></div></li>`
            a += `<span>¥${bdcuobj[i].teaprice}</span>`
            a += `<div>`
            a += `<button>→</button>`
            a += ` </div>`
            a += ` </div>`
            //在``里方法只能用单引号
            bodyobj.innerHTML = a;
        }
    }
})

//类型回显
Mock.mock("/js/typeshow", "post", function (type) {
    let bdcu = localStorage.Tea;
    let typenum = type.body
    let typeobj = JSON.parse(typenum)
    let bodyobj = document.getElementsByClassName("teashow")[0];
    if (bdcu == "" || bdcu == null || bdcu == undefined || bdcu == []) {
        bodyobj.innerHTML = "<div class='data'>暂无数据</div>";
        return false;
    } else {
        let bdcuobj = JSON.parse(bdcu)
        let a = "";
        bodyobj.innerHTML = ''
        for (let i = 0; i < bdcuobj.length; i++) {
            // if判断茶叶类型 
            if (bdcuobj[i].teatype == typeobj.type) {
                a += `<div>`
                a += ` <div class='teaimg'><img src='./img/${bdcuobj[i].teaimg}'></div>`
                a += `<li><a href="">${bdcuobj[i].teaname}</a><div class='teaunderline'></div></li>`
                a += `<span>¥${bdcuobj[i].teaprice}</span>`
                a += `<div>`
                a += `<button>→</button>`
                a += ` </div>`
                a += ` </div>`
                //在``里方法只能用单引号
                bodyobj.innerHTML = a;
            }
            if (typeobj.type == 7) {
                a += `<div>`
                a += ` <div class='teaimg'><img src='./img/${bdcuobj[i].teaimg}'></div>`
                a += `<li><a href="">${bdcuobj[i].teaname}</a><div class='teaunderline'></div></li>`
                a += `<span>¥${bdcuobj[i].teaprice}</span>`
                a += `<div>`
                a += `<button>→</button>`
                a += ` </div>`
                a += ` </div>`
                //在``里方法只能用单引号
                bodyobj.innerHTML = a;
            }
        }
        if( a == ""){
            bodyobj.innerHTML = "<div class='data'>暂无数据</div>";
            return false;
                 }
    }
})


//茶叶点击方法传参
Mock.mock("/js/teaclick", "post", function (xiabiao) {
    let xiaobiaobody = xiabiao.body
    // let xiabiao =JSON.parse(xiaobiaobody)
    localStorage.Xbiao=JSON.stringify(xiaobiaobody);
    return true
})

// 茶叶简介回显
Mock.mock("/js/teades", "post", function () {

    let teadata = localStorage.Tea;
    let teadataobj=JSON.parse(teadata);
    //获取点击的数据下标
    let num =localStorage.Xbiao
    let numobj = JSON.parse(num);
//书写类型

 let a= ""
 let b= ""
 let teashowhtml =document.getElementsByClassName("teashow")[0];
 let cydhlhtml =document.getElementsByClassName("cydhl")[0];
debugger
    for(let j=0;j<teadataobj.length;j++){
        if(j==numobj){ 
                        // if判断茶叶类型 d为类型
                        if (teadataobj[j].teatype == 0) {
                            var type = "绿茶";
                        } else if (teadataobj[j].teatype == 1) {
                            var type = "黄茶";
                        } else if (teadataobj[j].teatype == 2) {
                            var type = "乌龙茶";
                        } else if (teadataobj[j].teatype == 3) {
                            var type = "红茶";
                        } else if (teadataobj[j].teatype == 4) {
                            var type = "黑茶";
                        } else if (teadataobj[j].teatype == 5) {
                            var type = "白茶";
                        } else {
                            var type = "花茶";
                        }
            teashowhtml.innerHTML = " "
            cydhlhtml.innerHTML = " "
          a+=` <div class='imgbox'><img src='./img/${teadataobj[j].teaimg}' alt=''></div>`
          a+=  `<div class='title'>`
          a+= `<div><span class='teaname'>${teadataobj[j].teaname}</span></div>`
      a+=` <div class='liner'></div>`
      a+= `<div><span class='teaprice'>${teadataobj[j].teaprice}元</span></div>`
       a+=` <div class='p'>饮茶不但是传统饮食文化，同时，由于茶中含有多种抗氧化物质与抗氧化营养素，对于消除自由基有一定的效果。因此喝茶也有助防老，具养生保健功能，每天喝三两杯茶可起到防老的作用。茶叶中含有多种维生素和氨基酸，喝茶对于清油解腻，增强神经兴奋以及消食利尿也具有一定的作用。</div>`
       a+=` <div class='id'>商品id:${teadataobj[j].id}<span></span></div>`
       a+= `<div class='type1'>分类:<span class='type'>${type}</span></div>`
       a+= `<div class='gongneng'><button id='addShopCar' onclick='guce(\"${teadataobj[j].teaname}\")'>加入购物车</button></div>`
        // <!-- 可加图片 -->
       a+= `<div>收藏</div>`
    a+=`</div>`
   a+= `<div class='smimg'><img src='./img/${teadataobj[j].teaimg}' alt=''></div>`
  a+=` <div class='miaoshu'><div>描述</div></div>`
   a+= `<div class='longp'>饮茶不但是传统饮食文化，同时，由于茶中含有多种抗氧化物质与抗氧化营养素，对于消除自由基有一定的效果。因此喝茶也有助防老，具养生保健功能，每天喝三两杯茶可起到防老的作用。茶叶中含有多种维生素和氨基酸，喝茶对于清油解腻，增强神经兴奋以及消食利尿也具有一定的作用。 但并不是喝得越多越好，也不是所有的人都适合喝茶。一般来说，每天1-2次，每次3-5克、400毫升的饮量是比较适当的。一般，人们说喝茶还指喝水。</div>`
    a+=`<div class='bigimg'><img src='./img/茶叶简介通用.webp' alt=''></div>`
   a+=` <div class='xiangguan'>相关产品</div>`
   a+= `<div class='xgtea'>`
      a+= ` <div>`
            // <!-- 相关茶叶图片 -->
          a+=  `<div class='xgimg'><img src='./img/${teadataobj[j].teaimg}' alt=''></div>`
           a+= `<div class='tedian'>`
            a+=   ` <div>${teadataobj[j].teaname}</div>`
            a+=   ` <div>${teadataobj[j].teaprice}元</div>`
           a+= `</div>`
       a+=`</div>`
    a+=`</div>`
//导航栏
          b+=  `<div>`
          b+= `<a href="./茶叶.html">茶叶</a>`
          b+=` <span>></span>`
          b+= `<a href="./茶叶.html" onclick="typeshow(7)">全部</a>`
          b+= `<span>></span>`
          b+= `<a href="./茶叶.html" class="type" onclick='typeshow(${teadataobj[j].teatype})'>${type}</a>`
           b+=`<span>></span>`
           b+= `<span class="teaname">${teadataobj[j].teaname}</span>`
           b+= ` </div> `
           cydhlhtml.innerHTML = b;
    teashowhtml.innerHTML = a;
        }

    }
})

//首页茶叶
Mock.mock("/js/sytea", "post", function () {
    let bdcu = localStorage.Tea;
    let bodyobj = document.getElementsByClassName("tea")[0];
    if (bdcu == "" || bdcu == null || bdcu == undefined || bdcu == []) {
        bodyobj.innerHTML = "<div><div style='color:bule'>暂无数据</div></div>";
        return false;
    } else {
        let bdcuobj = JSON.parse(bdcu)
        let a = "";
        for (let i = 1; i < 7; i++) {
    
          a+=  `<div onclick='teaclick(\"${i}\")'>`
          a+=   `<div class='teaimg1'><img src='./img/img/${bdcuobj[bdcuobj.length-i].teaimg}' alt=''></div>`
          a+=   `<li>`
          a+=    `<a href="">${bdcuobj[bdcuobj.length-i].teaname}</a>`
          a+=    `<div class="teaunderline"></div>`
          a+=  ` </li>`
          a+=  ` <span>${bdcuobj[bdcuobj.length-i].teaprice}元</span>`
            a+= ` </div>`
            //在``里方法只能用单引号
        bodyobj.innerHTML = a;
        }
    }
})

// // 点击购物车
Mock.mock("/js/teaguce", "post", function (data) {
    let nametea =data.body
    let nametea1 = JSON.parse(nametea)
    let bdcu = localStorage.Tea;
    let shopcar =localStorage.shopCar;
    let shopbdcu =JSON.parse(bdcu);
    console.log(shopbdcu);
    for(let i =0;i<shopbdcu.length;i++){
        if(shopbdcu[i].teaname==nametea1){
            var guc = new Tea(1,shopbdcu[i].teaname,shopbdcu[i].teaimg,shopbdcu[i].teatype,shopbdcu[i].teaprice);
            if(shopcar == "" || shopcar == null || shopcar == undefined || shopcar == []){
                localStorage.shopCar=JSON.stringify(guc);
                alert("添加购物车成功");
                // location.href=('../购物车/view/购物车.html')
                break;
            }else{
                
                let shopcararr =JSON.parse(shopcar);
                for(let j =0;j<shopcararr.length;j++){
                    let name = shopcararr[0].teaname
                    if(name == nametea1){
                        alert("该茶叶已在购物车")
                        break;
                    }else{
                    // let guc = new Tea(shopcararr[i].id,shopcararr[i].teaname,shopcararr[i].teaimg,shopcararr[i].teatype,shopcararr[i].teaprice)
                                shopcararr.push(guc);
                                console.log(shopcararr);
                                localStorage.shopCar=JSON.stringify(shopcararr);
                                alert("添加购物车成功");
                                break;
                                // location.href=('../购物车/view/购物车.html')
                }
            }
        }
        } 
            }
    
           
    })